package com.library.servlet.admin;

import com.library.entity.po.BookCopyPO;
import com.library.entity.po.BookPO;
import com.library.entity.po.PagePO;
import com.library.service.admin.BookCopyService;
import com.library.service.admin.BookService;
import com.library.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/book")
public class BookServlet extends BaseServlet {

    private final BookService bookService = new BookService();
    private final BookCopyService bookCopyService = new BookCopyService();

    /**
     * 重定向至书籍副本管理页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void bookCopyManage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取分页参数
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        String keyword = request.getParameter("keyword");


        // 设置默认值
        int pageNum = 1;
        int pageSize = 5;

        if (pageNumStr != null && !pageNumStr.isEmpty()) {
            pageNum = Integer.parseInt(pageNumStr);
        }

        if (pageSizeStr != null && !pageSizeStr.isEmpty()) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PagePO<BookCopyPO> bookCopyPOPagePO = bookCopyService.pageBookCopy(pageNum, pageSize, keyword, request);
        request.setAttribute("bookCopyPOPagePO", bookCopyPOPagePO);

        request.getRequestDispatcher("/WEB-INF/admin/bookCopyManage.jsp").forward(request, response);
    }

    /**
     * 重定向至书籍管理页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void bookManage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取分页参数
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        String keyword = request.getParameter("keyword");

        System.out.println("分页参数: pageNum=" + pageNumStr + ", pageSize=" + pageSizeStr + "keyword=" + keyword);

        // 设置默认值
        int pageNum = 1;
        int pageSize = 5;

        if (pageNumStr != null && !pageNumStr.isEmpty()) {
            pageNum = Integer.parseInt(pageNumStr);
        }

        if (pageSizeStr != null && !pageSizeStr.isEmpty()) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PagePO<BookPO> bookPagePO = bookService.pageBook(pageNum, pageSize, keyword, request);

        request.setAttribute("bookPagePO", bookPagePO);

        request.getRequestDispatcher("/WEB-INF/admin/bookManage.jsp").forward(request, response);
    }


}
